有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么是日历。九月不起作用了?

我想看一个包含整天的月

private void createRandomData(InMemoryCursor cursor) {
    List<Object[]> data = new ArrayList<>();
    Calendar today = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
    today.set(Calendar.HOUR_OF_DAY,0);
    today.set(Calendar.MINUTE, 0);
    today.set(Calendar.SECOND, 0);
    today.set(Calendar.MILLISECOND, 0);
    mStart = (Calendar) today.clone();
    mStart.add(Calendar.SEPTEMBER, -5);
    while (mStart.compareTo(today) <= 0) {
        data.add(createItem(mStart.getTimeInMillis()));
        mStart.add(Calendar.SEPTEMBER, 1);
    }
    cursor.addAll(data);
}

当我写日历的时候。九月(或其他月份),我在日历上看到红线。其中包括:

Must be one of: Calendar.ERA, Calendar.YEAR, Calendar.MONTH, Calendar.WEEK_OF_YEAR, Calendar.WEEK_OF_MONTH, Calendar.DATE, Calendar.DAY_OF_MONTH, Calendar.DAY_OF_YEAR, Calendar.DAY_OF_WEEK, Calendar.DAY_OF_WEEK_IN_MONTH, Calendar.AM_PM, Calendar.HOUR, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND, Calendar.MILLISECOND, Calendar.ZONE_OFFSET, Calendar.DST_OFFSET less... (Ctrl+F1) This inspection looks at Android API calls that have been annotated with various support annotations (such as RequiresPermission or UiThread) and flags any calls that are not using the API correctly as specified by the annotations. Examples of errors flagged by this inspection: Passing the wrong type of resource integer (such as R.string) to an API that expects a different type (such as R.dimen). Forgetting to invoke the overridden method (via super) in methods that require it Calling a method that requires a permission without having declared that permission in the manifest Passing a resource color reference to a method which expects an RGB integer value. ...and many more. For more information, see the documentation at developer.安卓.com/tools/debugging/annotations.html

当我不顾红线运行它时,它会显示复杂的日期,如: see

我从GitHub使用这个库:https://github.com/jruesga/timeline-chart-view 这个问题和图书馆有关吗?还是关于Java日历


共 (2) 个答案

  1. # 1 楼答案

    您正在使用不兼容的选项。^{}的第一个参数是一个时间单位(天、周、小时等),由错误中列出的可能选项定义Calendar.SEPTEMBER不是时间单位,它是一个方便常数,表示9月的^{},通常在^{}方法中使用

    假设您在几个月内迭代,那么您将需要Calendar.MONTH

  2. # 2 楼答案

    @Michael's answer中所述,不能在add方法中使用Calendar.SEPTEMBER

    如果您想增加或减少指定的月数,只需使用Calendar.MONTH。如果要加/减天数,可以使用Calendar.DAY_OF_MONTH等等

    有时CalendarAPI可能会让人困惑(大多数时候,IMO),它有lots of problemsdesign issues

    在Android中,有一个更好的选择:可以使用ThreeTen Backport,这是Java8新的日期/时间类的一个很好的后端口。为了使它工作,您还需要ThreeTenABP(更多关于如何使用它here

    当您在默认时区中得到一个Calendar时,一个很好的替代对象是org.threeten.bp.ZonedDateTime(它表示特定时区中的日期和时间)。首先,我使用now()方法(以JVM默认时区的当前日期/时间为准)。然后我使用org.threeten.bp.LocalTime将时间设置为午夜

    我还使用minusMonths方法获取当前日期前5个月的日期,在循环中,我使用toInstant()方法获取毫秒值,使用plusMonths方法获取下一个月:

    // get today at default timezone, at midnight
    ZonedDateTime today = ZonedDateTime.now().with(LocalTime.MIDNIGHT);
    // 5 months ago
    ZonedDateTime start = today.minusMonths(5);
    while (start.compareTo(today) <= 0) {
        data.add(createItem(start.toInstant().toEpochMilli()));
        start = start.plusMonths(1);
    }
    

    例如,如果要添加/减去分钟而不是月份,可以使用方法minusMinutesplusMinutes。其他单位也有其他方法(如小时、天等),check the javadoc查看所有选项


    使用默认时区的问题在于它是can be changed without notice, even at runtime,因此最好总是明确显示您使用的时区

    使用Calendar,您可以使用TimeZone.getTimeZone(zoneName)

    Calendar todayCal = Calendar.getInstance(TimeZone.getTimeZone("Europe/London"), Locale.getDefault());
    

    使用Three-Ten Backport,您可以使用ZoneId.of(zoneName)

    ZonedDateTime today = ZonedDateTime.now(ZoneId.of("Europe/London")).with(LocalTime.MIDNIGHT);
    

    在上面的示例中,我使用了Europe/London,但是您可以将其更改为您想要的任何时区。API使用IANA timezones names(始终采用Region/City格式,如America/Sao_PauloEurope/Berlin)。 避免使用三个字母的缩写(如CSTPST),因为它们是ambiguous and not standard

    您可以通过调用ZoneId.getAvailableZoneIds()TimeZone.getAvailableIDs()获得可用时区的列表(并选择最适合您的系统的时区)